﻿using _1._6._2015_EntitiyFramework.Data.Models;
using _1._6._2015_EntityFramework.Data;
using _1_6_2014_Entity_Framework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _1_6_2014_Entity_Framework.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            OrdersDbContext db = new OrdersDbContext();
            List<Order> orders = db.Orders.ToList();



            // linq to objects
            List<OrderViewModel> orders = new List<OrderViewModel>();

            orders.Add(new OrderViewModel
                {
                    Name = "order 1",
                    OrderDate = DateTime.Now,
                    Total = 11.11
                });
            orders.Add(new OrderViewModel
            {
                Name = "order 2",
                OrderDate = DateTime.Now.AddDays(2),
                Total = 2.23
            });
            orders.Add(new OrderViewModel
            {
                Name = "order 3",
                OrderDate = DateTime.Now.AddHours(3),
                Total = 33.33
            });
            orders.Add(new OrderViewModel
            {
                Name = "order 4",
                OrderDate = DateTime.Now.AddHours(-5),
                Total = 59.44
            });
            orders.Add(new OrderViewModel
            {
                Name = "order 5",
                OrderDate = DateTime.Now.AddMonths(1),
                Total = 55.55
            });

            // Linq to entities

            // Filter a list of orders by total > 50 - extension method? linq to objects
            List<OrderViewModel> ordersGreaterThanfifty = orders
                .Where(o => o.Total > 50)
                .OrderByDescending(o => o.OrderDate)
                .ToList();

            // Same with LINQ syntax
            List<OrderViewModel> ordersGreaterThanfifty2 = (from o in orders
                                                           where o.Total > 50
                                                           orderby o.OrderDate descending
                                                           select o).ToList();





            // linq to objects
            List<Order> orders = new List<Order>();

            orders.Add(new OrderViewModel
            {
                Name = "order 1",
                OrderDate = DateTime.Now,
                Total = 11.11
            });
            orders.Add(new OrderViewModel
            {
                Name = "order 2",
                OrderDate = DateTime.Now.AddDays(2),
                Total = 2.23
            });
            orders.Add(new OrderViewModel
            {
                Name = "order 3",
                OrderDate = DateTime.Now.AddHours(3),
                Total = 33.33
            });
            orders.Add(new OrderViewModel
            {
                Name = "order 4",
                OrderDate = DateTime.Now.AddHours(-5),
                Total = 59.44
            });
            orders.Add(new OrderViewModel
            {
                Name = "order 5",
                OrderDate = DateTime.Now.AddMonths(1),
                Total = 55.55
            });

            // Filter a list of orders by total > 50 - extension method? linq to objects
            List<OrderViewModel> ordersGreaterThanfifty = orders
                .Where(o => o.Total > 50)
                .OrderByDescending(o => o.OrderDate)
                .ToList();

            // Same with LINQ syntax
            List<OrderViewModel> ordersGreaterThanfifty2 = (from o in orders
                                                            where o.Total > 50
                                                            orderby o.OrderDate descending
                                                            select o).ToList();













            OrdersDbContext db = new OrdersDbContext();
            IndexViewModel model = new IndexViewModel();

            model.Orders = db.Orders.Select(o => new OrderViewModel // doing projection
                {
                    OrderDate = o.OrderDate,
                    Total = o.Total,
                    Name = o.User.FirstName + " " + o.User.LastName,
                    OrderItems = db.OrderItems.Where(oi => oi.OrderId == o.OrderId).Select(oi => new OrderItemsViewModel
                    {
                        ItemDescription = oi.Item.Description,
                        Price = oi.Price.ToString()
                    }).ToList()
                }).ToList();

            // 2 things that trigger call to database - firstordefault or tolist. select builds list of sql but doesn't go to db yet

            return View(model);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}